home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Format / p2norm / p2norm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  8.4 KB  |  443 lines

  1. /* p2norm.c: p2 heading normalisation */
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Format/p2norm/RCS/p2norm.c,v 6.0 1991/12/18 20:20:24 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Format/p2norm/RCS/p2norm.c,v 6.0 1991/12/18 20:20:24 jpo Rel $
  9.  *
  10.  * $Log: p2norm.c,v $
  11.  * Revision 6.0  1991/12/18  20:20:24  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15.  
  16. #include "head.h"
  17. #include "util.h"
  18. #include "IOB-types.h"
  19. #include "or.h"
  20. #include "adr.h"
  21. #include <varargs.h>
  22. #include <isode/cmd_srch.h>
  23.  
  24. extern char    *dn2ufn();
  25. extern char    *local_dit;
  26. extern struct type_IOB_ORName    *orn2orname();
  27.  
  28. static void parse_cmdline();
  29.  
  30. main(argc, argv)
  31. int    argc;
  32. char    **argv;
  33. {
  34.     PE     pe = NULLPE;
  35.     PS    ps = NULLPS;
  36.     int    ishead = TRUE;
  37.  
  38.     sys_init(argv[0]);
  39.     or_myinit();
  40.     quipu_syntaxes();
  41.     dsap_init ((int *) NULL, (char ***) NULL);
  42.     local_dit = NULLCP; /* hack to get full DNs */
  43.     
  44.     parse_cmdline(argc, argv);
  45.  
  46.     if (((ps = ps_alloc (std_open)) == NULLPS) ||
  47.         (std_setup(ps, stdin) == NOTOK))
  48.     {
  49.         PP_LOG (LLOG_EXCEPTIONS, 
  50.             ("P22toP2() failed to setup inbound PS"));
  51.         return error_exit();
  52.     }
  53.  
  54.     if ((pe = ps2pe(ps)) == NULLPE)
  55.     {
  56.         PP_LOG (LLOG_EXCEPTIONS,
  57.             ("ps2pe error on input"));
  58.         return error_exit();
  59.     }
  60.  
  61.     if (ishead == TRUE) 
  62.         convert_p2_header(&pe);
  63.     else
  64.         convert_p2_ipn(&pe);
  65.  
  66.     if (ps) ps_free(ps);
  67.  
  68.     if (((ps = ps_alloc (std_open)) == NULLPS) ||
  69.         (std_setup(ps, stdout) == NOTOK))
  70.     {
  71.         PP_LOG (LLOG_EXCEPTIONS,
  72.             ("P22toP2() failed to setup outbound PS"));
  73.         return error_exit();
  74.     }
  75.     
  76.     if (pe2ps(ps, pe) == NOTOK) 
  77.     {
  78.         PP_LOG (LLOG_EXCEPTIONS,
  79.             ("pe2ps error on input"));
  80.         return error_exit();
  81.     }
  82.     
  83.     if (ps) ps_free(ps);
  84.     if (pe) pe_free(pe);
  85.     exit(0);
  86.     return OK;
  87. }
  88.  
  89. /*   */
  90. /* parse commmand line arguments */
  91. int    do_downgrade = FALSE, do_normalise = TRUE, internal = FALSE;
  92.  
  93. #define ARG_NONORM    1
  94. #define ARG_DOWNGRADE    2
  95. #define ARG_EXTERNAL    3
  96. #define ARG_INTERNAL    4
  97.  
  98. static CMD_TABLE tbl_args[] = {
  99.     "-nonorm",    ARG_NONORM,
  100.     "-downgrade", ARG_DOWNGRADE,
  101.     "-external",    ARG_EXTERNAL,
  102.     "-internal",    ARG_INTERNAL,
  103.     0, -1
  104. };
  105.  
  106. static void parse_cmdline(argc, argv)
  107. int    argc;
  108. char    **argv;
  109. {
  110.     int i;
  111.     
  112.     for (i = 1; i < argc; i++) {
  113.         switch (cmd_srch(argv[i],
  114.                  tbl_args)) {
  115.             case ARG_NONORM:
  116.             do_normalise = FALSE;
  117.             break;
  118.             
  119.             case ARG_DOWNGRADE:
  120.             do_downgrade = TRUE;
  121.             break;
  122.  
  123.             case ARG_EXTERNAL:
  124.             internal = FALSE;
  125.             break;
  126.  
  127.             case ARG_INTERNAL:    
  128.             internal = TRUE;
  129.             break;
  130.  
  131.             default:
  132.             PP_LOG(LLOG_EXCEPTIONS,
  133.                    ("Unknown command line argument '%s'",
  134.                 argv[i]));
  135.             break;
  136.         }
  137.     }
  138. }
  139.  
  140. /*   */
  141. /* convert header */
  142.  
  143. convert_p2_header(ppe)
  144. PE    *ppe;
  145. {
  146.     struct type_IOB_Heading    *head;
  147.  
  148.     PY_pepy[0] = 0;
  149.     
  150.     if (decode_IOB_Heading (*ppe, 0, NULLIP, NULLVP, &head) != OK) 
  151.     {
  152.         PP_OPER(NULLCP,
  153.             ("decode_IOB_Heading() failed : [%s]", PY_pepy));
  154.         return error_exit();
  155.     }
  156.         
  157.     if (PY_pepy[0] != 0)
  158.         PP_LOG (LLOG_EXCEPTIONS,
  159.             ("decode_IOB_Heading non-fatal failure [%s]",PY_pepy)); 
  160.     
  161.     convert_header(head);
  162.  
  163.     if (*ppe) pe_free(*ppe);
  164.     
  165.     PY_pepy[0] = 0;
  166.  
  167.     if (encode_IOB_Heading (ppe, 0, 0, NULLCP, head) != OK)
  168.     {
  169.         PP_OPER(NULLCP,
  170.             ("encode_IOB_Heading() failed : [%s]", PY_pepy));
  171.         return error_exit();
  172.     }
  173.     if (PY_pepy[0] != 0)
  174.         PP_LOG (LLOG_EXCEPTIONS,
  175.             ("encode_IOB_Heading non-fatal failure [%s]", PY_pepy));
  176.     return OK;
  177. }
  178.  
  179. convert_header(hd)
  180. struct type_IOB_Heading    *hd;
  181. {
  182.     if (hd -> this__IPM)
  183.         convert_IPMIdentifier (hd -> this__IPM);
  184.     
  185.     if (hd -> originator)
  186.         convert_ORDescriptor(hd -> originator);
  187.  
  188.     if (hd -> authorizing__users)
  189.         convert_ORDescriptorSeq(hd -> authorizing__users);
  190.  
  191.     if (hd -> primary__recipients)
  192.         convert_RecipientSeq(hd -> primary__recipients);
  193.  
  194.     if (hd -> copy__recipients)
  195.     convert_RecipientSeq(hd -> copy__recipients);
  196.     
  197.     if (hd -> blind__copy__recipients)
  198.         convert_RecipientSeq(hd -> blind__copy__recipients);
  199.  
  200.     if (hd -> replied__to__IPM)
  201.         convert_IPMIdentifier (hd -> replied__to__IPM);
  202.  
  203.     if (hd -> obsoleted__IPMs)
  204.         convert_IPMIdentifierSeq (hd -> obsoleted__IPMs);
  205.  
  206.     if (hd -> related__IPMs)
  207.         convert_IPMIdentifierSeq (hd -> related__IPMs);
  208.  
  209.     if (hd -> reply__recipients)
  210.         convert_ORDescriptorSeq(hd -> reply__recipients);
  211.  
  212.     if (do_downgrade == TRUE && hd -> extensions) {
  213.         free_IOB_ExtensionsField(hd -> extensions);
  214.         hd -> extensions = (struct type_IOB_ExtensionsField *) NULL;
  215.     }
  216. }
  217.  
  218. convert_ORDescriptorSeq (seq)
  219. struct type_IOB_ORDescriptorSequence    *seq;
  220. {
  221.     while (seq != (struct type_IOB_ORDescriptorSequence *) NULL) {
  222.         if (seq -> ORDescriptor)
  223.             convert_ORDescriptor (seq -> ORDescriptor);
  224.         seq = seq -> next;
  225.     }
  226. }
  227.  
  228. convert_RecipientSeq (seq)
  229. struct type_IOB_RecipientSequence    *seq;
  230. {
  231.     while (seq != (struct type_IOB_RecipientSequence *) NULL) {
  232.         if (seq -> RecipientSpecifier)
  233.             convert_RecipientSpecifier (seq -> RecipientSpecifier);
  234.         seq = seq -> next;
  235.     }
  236. }
  237.  
  238. convert_RecipientSpecifier (recip)
  239. struct type_IOB_RecipientSpecifier    *recip;
  240. {    
  241.     if (recip -> recipient)
  242.         convert_ORDescriptor(recip -> recipient);
  243. }
  244.  
  245. convert_IPMIdentifierSeq (seq)
  246. struct type_IOB_IPMIdentifierSequence *seq;
  247. {
  248.     while (seq != (struct type_IOB_IPMIdentifierSequence *) NULL) {
  249.         if (seq -> IPMIdentifier)
  250.             convert_IPMIdentifier(seq -> IPMIdentifier);
  251.         seq = seq -> next;
  252.     }
  253. }
  254.     
  255. convert_IPMIdentifier (ipmid)
  256. struct type_IOB_IPMIdentifier    *ipmid;
  257. {
  258.     char    *str;
  259.  
  260.     if (ipmid -> user) {
  261.         convert_ORName (&(ipmid -> user),
  262.                 &str);
  263.         if (str) free(str);
  264.     }
  265. }
  266.  
  267. convert_ORName (pname, pdn)
  268. struct type_IOB_ORName    **pname;
  269. char **pdn;
  270. {
  271.     ORName    *orn;
  272.     
  273.     *pdn = NULLCP;
  274.  
  275.     if ((orn = orname2orn (*pname)) == NULLORName)
  276.         return;
  277.  
  278.     if (do_normalise == TRUE) {
  279.         Aparse_ptr    ap = aparse_new();
  280.         
  281.         ap->orname->on_or = or_tdup(orn->on_or);
  282.         ap -> ad_type = AD_X400_TYPE;
  283.         ap -> dmnorder = CH_USA_PREF;
  284.         ap -> normalised = APARSE_NORM_NEXTHOP;
  285.         ap -> percents = TRUE;
  286.         ap -> internal = internal;
  287.         if (aparse_norm (ap) == OK) {
  288.             or_free(orn->on_or);
  289.             orn->on_or = ap->orname->on_or;
  290.             ap->orname->on_or = NULLOR;
  291.         }
  292.         aparse_free(ap);
  293.         free((char *) ap);
  294.     }
  295.  
  296.     if (do_downgrade == TRUE) {
  297.         /* downgrade orn -> on_or */
  298.         or_downgrade(&(orn->on_or));
  299.  
  300.         if ((*pname) -> directory__name && orn->on_dn != NULL) {
  301.             *pdn = dn2ufn(orn->on_dn, FALSE);
  302.             dn_free (orn->on_dn);
  303.             orn->on_dn = NULL;
  304.         }
  305.     }
  306.  
  307.     free_IOB_ORName (*pname);
  308.     *pname = orn2orname(orn);
  309. }
  310.  
  311. convert_ORDescriptor (desc)
  312. struct type_IOB_ORDescriptor    *desc;
  313. {
  314.     char    *dn;
  315.  
  316.     if (!desc || !desc -> formal__name) 
  317.         return;
  318.  
  319.     convert_ORName (&(desc->formal__name), &dn);
  320.  
  321.     if (dn != NULLCP) {
  322.         char    *ffn, *res;
  323.         int    len;
  324.         if (desc -> free__form__name != NULL) {
  325.             /* incorporate dn as comment in free__form__name */
  326.             ffn = qb2str (desc -> free__form__name);
  327.             free_IOB_FreeFormName (desc -> free__form__name);
  328.         } else
  329.             ffn = NULLCP;
  330.  
  331.         len = strlen(dn) + strlen(" (DN=)") + 1;
  332.         if (ffn != NULLCP)
  333.             len += strlen(ffn);
  334.         res = malloc(len * sizeof(char));
  335.  
  336.         if (ffn == NULLCP)
  337.             sprintf(res, "(DN=%s)", dn);
  338.         else
  339.             sprintf(res, "%s (DN=%s)", ffn, dn);
  340.         
  341.         desc -> free__form__name = 
  342.             str2qb(res, strlen(res), 1);
  343.         if (dn) free(dn);
  344.         if (ffn) free(ffn);
  345.         if (res) free(res);
  346.     }
  347. }
  348.  
  349. /*   */
  350. /* convert ipn */
  351.  
  352. convert_p2_ipn(ppe)
  353. PE    *ppe;
  354. {
  355.     struct type_IOB_IPN    *head;
  356.  
  357.     PY_pepy[0] = 0;
  358.  
  359.     if (decode_IOB_IPN (*ppe, 0, NULLIP, NULLVP, &head) != OK) 
  360.     {
  361.         PP_OPER(NULLCP,
  362.             ("decode_IOB_IPN() failed : [%s]", PY_pepy));
  363.         return error_exit();
  364.     }
  365.         
  366.     if (PY_pepy[0] != 0)
  367.         PP_LOG (LLOG_EXCEPTIONS,
  368.             ("decode_IOB_IPN non-fatal failure [%s]",PY_pepy)); 
  369.     
  370.     convert_ipn(head);
  371.  
  372.     if (*ppe) pe_free(*ppe);
  373.     
  374.     PY_pepy[0] = 0;
  375.  
  376.     if (encode_IOB_IPN (ppe, 0, 0, NULLCP, head) != OK)
  377.     {
  378.         PP_OPER(NULLCP,
  379.             ("encode_IOB_IPN() failed : [%s]", PY_pepy));
  380.         return error_exit();
  381.     }
  382.     if (PY_pepy[0] != 0)
  383.         PP_LOG (LLOG_EXCEPTIONS,
  384.             ("encode_IOB_IPN non-fatal failure [%s]", PY_pepy));
  385.     return OK;
  386. }
  387.  
  388. convert_ipn(ipn)
  389. struct type_IOB_IPN *ipn;
  390. {
  391.  
  392.     if (ipn -> subject__ipm)
  393.         convert_IPMIdentifier (ipn -> subject__ipm);
  394.  
  395.     if (ipn -> ipn__originator)
  396.         convert_ORDescriptor (ipn -> ipn__originator);
  397.     
  398.     if (ipn -> ipn__preferred__recipient)
  399.         convert_ORDescriptor (ipn -> ipn__preferred__recipient);
  400.  
  401.     /* should really do something about ReturnedIPMField 
  402.        in non recipt fields but will assume message has 
  403.        been exploded */
  404. }
  405.  
  406. /*   */
  407. /* misc */
  408.  
  409. error_exit()
  410. {
  411.     exit(1);
  412. }
  413.  
  414. #ifndef lint
  415.  
  416. void    advise (va_alist)
  417. va_dcl
  418. {
  419.     int     code;
  420.     va_list ap;
  421.  
  422.     va_start (ap);
  423.  
  424.     code = va_arg (ap, int);
  425.  
  426.     (void) _ll_log (pp_log_norm, code, ap);
  427.  
  428.     va_end (ap);
  429. }
  430.  
  431. #else
  432. /* VARARGS */
  433.  
  434. void    advise (code, what, fmt)
  435. char    *what,
  436.         *fmt;
  437. int      code;
  438. {
  439.     advise (code, what, fmt);
  440. }
  441. #endif
  442.  
  443.